package com.tcwlkj.uuService.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tcwlkj.uuService.R;

/**
 * Created by Administrator on 2017/11/24 0024.
 */

public class MyDialog extends Dialog {
    private ClickListenerInterface clickListenerInterface;
    private EditText password;

    public interface ClickListenerInterface {
        void  doConfirm(String msg);
        void  doCancel();
    }

    private LinearLayout  llEmpty;

    private String QiangDanMsg;
    public MyDialog(Context context,String msg) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);
        this.QiangDanMsg=msg;
    }

    public MyDialog(Context context, int themeResId,String msg) {
        super(context, themeResId);
        this.QiangDanMsg=msg;
    }

    protected MyDialog(Context context, boolean cancelable, OnCancelListener cancelListener,String msg) {
        super(context, cancelable, cancelListener);
        this.QiangDanMsg=msg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ll_empty);
        llEmpty = (LinearLayout) findViewById(R.id.ll_empty);//一个空布局
        if (llEmpty != null) {
            llEmpty.removeAllViews();
        }
        if(QiangDanMsg.equals("YZM")){
            showYZM();
        }else if(QiangDanMsg.equals("CANCLE")){
            applyCancel();
        }else {
            IsSucess(QiangDanMsg);//抢单成功
        }
    }
    //抢单成功
    public void IsSucess( String msg) {
        View view = View.inflate(getContext(), R.layout.my_dialog, null);
        llEmpty.addView(view);
        Button btnDialog=(Button) view.findViewById(R.id.btn_dialogbase);
        TextView tv_content_dialogbase= (TextView) view.findViewById(R.id.tv_content_dialogbase);
        btnDialog.setOnClickListener(new clickListener2());
        tv_content_dialogbase.setText(msg);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.7); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    //验证码
    EditText etYzm;
    public void showYZM() {
        View view = View.inflate(getContext(), R.layout.dialog_edit, null);
        llEmpty.addView(view);
        Button btnClick = (Button) view.findViewById(R.id.btn_sure_dialogedit);
        etYzm=(EditText) view.findViewById(R.id.edit_checknum_dialogedit);
        btnClick.setOnClickListener(new clickListener2());
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.7); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    //解约
    TextView tv_title;
    public void applyCancel() {
        View view = View.inflate(getContext(), R.layout.dialog_cancle, null);
        llEmpty.addView(view);
        tv_title= (TextView) view.findViewById(R.id.tv_title);
        TextView btnClick = (TextView) view.findViewById(R.id.cancel);//确定
        TextView submit = (TextView) view.findViewById(R.id.submit);//取消
        password = (EditText) view.findViewById(R.id.edit_checknum_dialogedit);
        btnClick.setOnClickListener(new clickListener2());
        submit.setOnClickListener(new clickListener2());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.85); // 宽度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    public void setText(String text){
        tv_title.setText(text);
    }

    public void setEditext(){
        password.setHint("请填写解约原因...");
        password.setSingleLine(false);
        password.setInputType(InputType.TYPE_CLASS_TEXT);
        password.setSingleLine(false);
        password.setMinLines(3);
        password.setGravity(0);
    }
    public void setEditextHeight(){
        password.setHeight(100);
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface2) {
        this.clickListenerInterface = clickListenerInterface2;
    }

    private class clickListener2 implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.btn_dialogbase:
                    clickListenerInterface.doConfirm("");
                    break;
                case R.id.btn_sure_dialogedit:
                    clickListenerInterface.doConfirm(etYzm.getText().toString().trim());
                    break;
                case R.id.cancel:
                    clickListenerInterface.doConfirm(password.getText().toString().trim());
                    break;
                case R.id.submit:
                    clickListenerInterface.doCancel();
                    break;
            }
        }
    }
}
